//
//  HTTextView.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/26.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit

class HTTextView: UITextView {

    //提供一个外界可以访问到的文字
    var placeholder: String?{
        didSet{
        
            placeholderLbale.text = placeholder
        }
        
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        addSubview(placeholderLbale)
        
        placeholderLbale.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(self).offset(-10)
        }
        
        
        //使用通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChang", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    @objc private func textDidChang(){
    
        placeholderLbale.hidden = hasText()
    }
    
    private lazy var placeholderLbale : UILabel = {
    
        let  label = UILabel()
        label.text = "这里真的他妈的烦热啊,谁发明的,吃屎吧"
        label.alpha = 0.4
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()

    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
